<!--
 * @Author: 赵成刚-货源单
-->
<template>
  <view class="content">
    <view class="search-box">
      <view class="search-input">
        <u-search
          :height="56"
          bg-color="transparent"
          :show-action="false"
          placeholder="请输入货源单号"
          @search="handleSearch"
          @clear="handleSearchClear"
          v-model="searchValue"
        ></u-search>
      </view>
      <image
        class="search-icon2"
        @click="searchQrcode"
        src="../../../static/search-icon2.png"
      />
      <view class="search-btn" @click="popSearchShow = true">筛选</view>
    </view>
    <view>
      <view class="card-list" v-if="dataList">
        <view
          v-for="(item1, index) in dataList"
          :key="index"
          style="margin-bottom: 20rpx"
        >
          <C_myRecommendation
            :myRecommendation="item1"
            :dataSourcesType="11"
            :footBtnsShow="true"
            :pageList="'sourcegoods'"
            @routeJumpBtn="routeJumpBtn"
            @grabAnOrder="grabAnOrder"
          />
        </view>
      </view>
      <u-empty
        text="列表为空"
        class="kong"
        mode="list"
        v-if="dataList.length === 0"
      ></u-empty>
      <u-loadmore
        v-if="dataList.length"
        bg-color="rgb(240, 240, 240)"
        :status="loadStatus"
      ></u-loadmore>
    </view>
    <popSearchModal
      :popSearchShow="popSearchShow"
      @getDataList="searchBtn"
      @closePop="closePop"
      class="popModal"
      :page="'sourcegoods'"
    />
    <C_tabbar
      :routePath="'pages/drivertabbar/sourcegoods/sourcegoods'"
      class="tabbar-class"
    ></C_tabbar>
    <loginTip
      :loginTipShow="loginTipShow"
      @confirm="loginTipConfirm"
      @cancel="loginTipCancel"
    />
    <certificateTip
      :certificateTipShow="certificateTipShow"
      @confirm="certificateTipConfirm"
      @cancel="certificateTipCancel"
    />
    <!-- #ifdef MP-WEIXIN -->
    <zero-privacy
      :onNeed="false"
      :hideTabBar="true"
      @disagree="quit"
      @agree="agreeChange"
    ></zero-privacy>
    <!-- #endif -->
  </view>
</template>

<script>
import { d_resNull } from 'common/util.js'
import C_myRecommendation from './../../../components/C_myRecommendation/index'
import C_tabbar from './../../../components/C_tabbar/index'
import loginTip from '../../../components/C_tipModal/loginTip.vue'
import popSearchModal from '../../../components/C_popSearch/index.vue'
import certificateTip from '../../../components/C_tipModal/certificateTip.vue'

export default {
  components: {
    C_tabbar,
    C_myRecommendation,
    loginTip,
    certificateTip,
    popSearchModal,
  },
  data() {
    return {
      searchValue: '',
      popSearchShow: false,
      loginTipShow: false,
      certificateTipShow: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loadStatus: 'loadmore',
      searchObj: {},
      dataList: [], // 全部
      timeshow: false,
      regionshow: false,
      userInfo: uni.getStorageSync('userInfo'),
    }
  },
  onNavigationBarButtonTap(e) {
    if (e.text == '筛选') {
      this.popSearchShow = true
    }
  },
  onReachBottom() {
    if (this.taglistShow) {
      this.showLoading()
      this.loadStatus = 'loading'
      setTimeout(() => {
        this.pageNum++
        uni.stopPullDownRefresh()
        this.getDataList()
        this.loadStatus = 'nomore'
      }, 1000)
    } else {
      this.loadStatus = 'nomore'
    }
  },
  // 强制下拉刷新
  onPullDownRefresh() {
    this.pageNum = 1
    this.pageSize = 10
    // this.showLoading()
    setTimeout(() => {
      uni.stopPullDownRefresh()
      this.getDataList()
    }, 1000)
  },
  onShow() {
    uni.hideTabBar({})
    this.getDataList()
  },

  methods: {
    // 隐私授权
    quit() {
      wx.exitMiniProgram({
        success: (res) => {
          uni.$u.toast('即将退出小程序')
        },
      })
    },
    // 同意隐私授权
    agreeChange() {
      // 同意后所执行的代码逻辑，看需添加此方法。
    },
    closePop() {
      this.popSearchShow = false
    },
    showLoading() {
      uni.showLoading({
        title: '加载中',
      })
    },
    hideLoading() {
      uni.hideLoading()
    },
    grabAnOrder(info) {
      if (Object.keys(this.userInfo).length === 0) {
        return (this.loginTipShow = true)
      }
      if (this.userInfo.authStatus == null || this.userInfo.authStatus != 1) {
        return (this.certificateTipShow = true)
      } else {
        uni.navigateTo({
          url: `/pages/drivertabbar/sourcegoods/detail?sourceId=${info}`,
        })
      }
    },
    //搜索
    searchBtn(searchObj) {
      this.searchObj = Object.assign(this.searchObj, searchObj)
      this.pageNum = 1
      this.pageSize = 10

      uni.pageScrollTo({
        scrollTop: 0,
        duration: 0,
      })
      this.getDataList()
      this.closePop()
    },
    //清空搜索
    clearBtn() {
      this.searchObj.createTime = ''
      this.searchObj.loadPlace = ''
      this.searchObj.targetPlace = ''
      this.searchObj.sourceCode = ''
      this.pageNum = 1
      this.pageSize = 10

      this.getDataList()
    },
    loginTipConfirm() {
      this.loginTipShow = false
    },
    loginTipCancel() {
      this.loginTipShow = false
    },
    certificateTipConfirm() {
      this.certificateTipShow = false
    },
    certificateTipCancel() {
      this.certificateTipShow = false
    },
    //跳转到详情页面
    routeJumpBtn(info) {
      uni.navigateTo({
        url: `/pages/drivertabbar/sourcegoods/detail?sourceId=${info.sourceId}`,
      })
    },
    //根据登录用户查询货源单
    async getDataList() {
      const params = {
        sourceStatus: 1,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ...this.searchObj,
      }
      const { code, result, message } = await this.$u.get(
        '/freight-business/freightSource/driver/app/sourcePage',
        params
      )
      if (code === '0') {
        this.hideLoading()
        this.taglistShow = result.records.length ? true : false
        this.loadStatus =
          result.records.length < this.pageSize ? 'nomore' : 'loadmore'
        if (this.pageNum == 1) {
          this.dataList = result.records
        } else {
          this.dataList = this.dataList.concat(d_resNull(result.records))
        }
        this.total = result.total
      } else {
        this.hideLoading()
        this.$api.msg(message)
      }
    },
    handleSearch(val) {
      this.searchObj.sourceCode = val
      this.pageNum = 1
      this.pageSize = 10
      this.getDataList()
    },
    handleSearchClear() {
      this.searchObj.sourceCode = ''
      this.pageNum = 1
      this.pageSize = 10
      this.getDataList()
    },
    searchQrcode() {
      uni.scanCode({
        success: function (res) {
          uni.navigateTo({
            url: `/pages/drivertabbar/sourcegoods/detail?sourceId=${res.result}`,
          })
        },
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  position: relative;
  z-index: 11;
  min-height: 1600rpx;
  background-color: rgb(238, 238, 239);
  .tabs-cont {
    position: fixed;
    left: 0;
    width: 100%;
    z-index: 10;
  }
  .search-u-search-box {
    display: flex;
    align-items: center;
    background-color: #ffff;
    padding: 0 30rpx;
    .u-search {
      padding: 16rpx 0;
      /deep/.u-content {
        border: 1rpx solid #979797;
      }
    }
  }
  .card-list {
    padding-top: 10rpx;
    padding-bottom: 80rpx;
    margin: 0 30rpx;
  }
}
.kong {
  padding: 200rpx 0;
}
.tabbar-class {
  position: fixed;
  z-index: 11;
}
.search-box {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0 30rpx;
}
.search-input {
  position: relative;
  width: 518rpx;
  height: 56rpx;
  background: #ffffff;
  border-radius: 28rpx;
  border: 1px solid #979797;
}
.search-icon1 {
  position: absolute;
  left: 17rpx;
  top: 0;
  bottom: 0;
  margin: auto 0;
  width: 30rpx;
  height: 30rpx;
}
.search-icon2 {
  width: 44rpx;
  height: 44rpx;
  margin: 0 30rpx;
}
.search-btn {
  font-size: 28rpx;
  color: #6190e8;
  line-height: 28rpx;
}
</style>
